<?php

namespace AppBundle\Controller;

use AppBundle\Entity\Profile;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use AppBundle\Entity\User;
use AppBundle\Form\UserType;
//use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
//use Symfony\Component\BrowserKit\Request;
//use Symfony\Component\BrowserKit\Response;


class UserController extends Controller
{
    /**
     * @Route("/user/test")
     * @return Response
     */
    public function testAction()
    {
        return new Response("user de action");
    }

    /**
     * @param $id;
     * @Route("/")
     */
    public function indexAction($id)
    {
        $em = $this->getDoctrine()->getManager();
        $user = $em->getRepository(AppBundle::User)->findBy($id);

        return new Response($user[0]);

    }

    /**
     * @Route("/user/add")
     * @Method({"POST"})
     * @param Request $request
     * @return Response
     */
    public function addAction(Request $request)
    {
        $user = new User();
        $form = $this->createForm(new UserType(),$user);
        $form->handleRequest($request);
        if($form->isSubmitted()&&$form->isValid())
        {
            $em = $this->getDoctrine()->getManager();
            $em->persist($user);
            $em->flush();

            return new Response("ok");
        }

        return new Response("something is wrong");

    }

    /**
     * @Route("/useradd")
     * @return Response
     */
    public function userAddAction()
    {
        $user = new User();
        $profile = new Profile();
        $profile->setAge(22);
        $profile->setPhone(18221759664);
        $user->setUsername("bb");
        $user->setPassword("secret");
        $user->setProfile($profile);

        $em = $this->getDoctrine()->getManager();
        $em->persist($user);
        $em->persist($profile);
        $em->flush();

        return new Response("ok");
    }
}